package work5;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import testdruid.DruidUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBCUtilsByProperties {
    public static  String URL = "";
    public static  String USER = "";
    public static  String PASSWORD = "";
    private Connection mConnection;
    private Statement mStatement;

    static {

        try {
            //初始化一个properties对象
            Properties properties = new Properties();
            //获胜我们自定义的db.properties的流对象
            InputStream inputStream = new FileInputStream(new File("C:\\Users\\Administrator\\IdeaProjects\\untitled\\src\\work5\\db.properties"));
            //通过properties对象读取这个流
            properties.load(inputStream);

            //读取完后获取其中信息
            URL =  properties.getProperty("url");
            USER = properties.getProperty("username");
            PASSWORD =  properties.getProperty("password");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //获取连接
    public static Connection getConnection(){
        try {
            Connection connection = DriverManager.getConnection(URL,USER,PASSWORD);

            return connection;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    //关闭连接
    public static void close(Connection connection,Statement statement)
    {
        try {

            if (null != statement)statement.close();
            if (null != connection) connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    //关闭带有resultSet的连接
    public static void close(Connection connection, Statement statement, ResultSet resultSet)
    {
        try {
            if (null != resultSet) resultSet.close();
            if (null != statement)statement.close();
            if (null != connection) connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
